package com.jiuli_travel.goods_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jiuli_travel.attraction_service.model.entity.Order;
import com.jiuli_travel.goods_service.model.dto.goods.GoodsInsertRequest;
import com.jiuli_travel.goods_service.model.dto.goods.PurchaseGoodsRequest;
import com.jiuli_travel.goods_service.model.dto.order.CreateOrderRequest;
import com.jiuli_travel.goods_service.model.entity.Goods;
import com.jiuli_travel.goods_service.mapper.GoodsMapper;
import com.jiuli_travel.goods_service.model.enums.PurchaseMethodEnum;
import com.jiuli_travel.goods_service.service.ICartService;
import com.jiuli_travel.goods_service.service.IGoodsOrdersService;
import com.jiuli_travel.goods_service.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuli_travel.user_service.model.enums.UserRoleEnum;
import com.jiuli_travel.user_service.model.vo.UserVO;
import com.jiuli_travel.utils.Result;
import com.jiuli_travel.utils.UserHolder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiuli
 * @since 2024-05-06
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 发布商品
     */
    @Override
    public Result publishGoods(GoodsInsertRequest goodsInsertRequest){
        // 获取登录用户的基础信息
        UserVO loginUser = UserHolder.getUser();

        // valid（用户是有权限的才可以进行发布商品）
        if (!UserRoleEnum.SHOP.getValue().equals(loginUser.getUserRole())){
            return Result.fail("无权限进行该操作");
        }

        // 转换数据格式
        Goods goods = BeanUtil.copyProperties(goodsInsertRequest, Goods.class);
        goods.setUserId(loginUser.getId());

        // 自己发布的商品的名称不能重名
        Long count = lambdaQuery()
                .eq(Goods::getName, goods.getName())
                .eq(Goods::getUserId, goods.getId())
                .count();
        if(count > 0){
            return Result.fail("该商品名称已使用过");
        }

        // 发布
        boolean save = save(goods);
        return Result.ok("发布商品成功");
    }

    /**
     * 下架商品
     * @param goodsId 商品id
     * @return
     */
    @Override
    public Result shelvesGoods(Long goodsId) {
        // 获取登录用户的基础信息
        UserVO loginUser = UserHolder.getUser();

        // 查询回来该商品的信息
        Goods goods = getById(goodsId);

        // 校验是否符合下架的要求
        // 1. 是否是自己的商品或者管理员
        if(!(goods.getUserId().equals(loginUser.getId()) || UserRoleEnum.ADMIN.getValue().equals(loginUser.getUserRole()))){
            return Result.fail("无权限下架该商品");
        }
        // ...

        // 下架该商品
        boolean removeFlag = removeById(goodsId);

        return Result.ok("下架成功");
    }

    /**
     * search商品信息
     */
    public Result searchGoods(String searchText, Integer from, Integer size) throws IOException {
        // 1. 从es里面获取数据
        // 1.1 准备请求
        SearchRequest goodsEsSearch = new SearchRequest("goods_es");

        // 1.2 书写DSL
        goodsEsSearch.source()
                .from(from).size(size)
                .query(QueryBuilders.matchQuery("name", searchText));

        // 1.3 发起请求
        SearchResponse searchResponse = restHighLevelClient.search(goodsEsSearch, RequestOptions.DEFAULT);

        // 2. 解析响应结果
        System.out.println(searchResponse);
        return null;
    }

}












